/**    
 * 文件名：JavaCVTest.java    
 *    
 * 版本信息：    
 * 日期：2014年3月6日    
 * xyj 足下 xyj 2014     
 * 版权所有    
 *    
 */
import static com.googlecode.javacv.cpp.opencv_highgui.CV_LOAD_IMAGE_COLOR;
import static com.googlecode.javacv.cpp.opencv_highgui.CV_LOAD_IMAGE_GRAYSCALE;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static opencvtest.OpenCVUtils.loadAndShowOrExit;
import static opencvtest.OpenCVUtils.show;

import java.io.File;

import com.googlecode.javacv.cpp.opencv_core;
import opencvtest.chapter04.ColorHistogram;

import com.googlecode.javacv.cpp.opencv_core.IplImage;

/**
 * @项目名称：opencv-test
 * @类名称：JavaCVTest
 * @类描述：
 * @创建人：zhuyi
 * @创建时间：2014年3月6日 下午5:11:42
 * @修改人：zhuyi
 * @修改时间：2014年3月6日 下午5:11:42
 * @修改备注：
 * @version
 * 
 */
public class JavaCVTest {
    public static void main(String[] args) {

//        IplImage image = loadAndShowOrExit(new File("art5014360081-s.jpg"),
//                CV_LOAD_IMAGE_COLOR);
//
//        System.out.println(image.nChannels());
//
//        opencv_core.CvMat mat = image.asCvMat();
//
//        System.out.println(mat.rows());
//        System.out.println(mat.cols());
//
//        for (int i = 126;i< 150;i++) {
//            for (int j = 150; j < 200;j++) {
//                int v = (int) mat.get(i,j);
//                System.out.println(v);
//                if (v > 240) {
//                    mat.put(i, j, 0);
//                }
//
//
//
//            }
//        }
//
//
//        show(mat, "dealed");

//        System.out.println(mat);

//        IplImage[] channels = ColorHistogram.splitChannels(image);
//
//        int count = 0;
//        for (IplImage channel : channels) {
//            show(channel, "" + count++);
//        }

        // Read input images
//        IplImage image1 = loadAndShowOrExit(new File("data/boldt.jpg"), CV_LOAD_IMAGE_COLOR);
//        IplImage image2 = loadAndShowOrExit(new File("data/rain.jpg"), CV_LOAD_IMAGE_COLOR);
//
//        CvMat mat1 = image1.asCvMat();
//        CvMat mat2 = image2.asCvMat();
//
//        System.out.println(mat1.nChannels());
//        System.out.println(mat2.nChannels());
//
////        IplImage result = IplImage.create(cvGetSize(image1), image1.depth(), image1.depth());
//        CvMat resultMat = CvMat.create(mat1.rows(), mat2.cols(), image1.depth(), image1.nChannels());
//
//        int n = mat1.rows() * mat1.cols() * mat1.nChannels();
//
//        for (int i = 0; i < n; i++) {
//            int v1 = (int) mat1.get(i);
//            int v2 = (int) mat2.get(i);
//            resultMat.put(i, v1 * 0.7 + v2 * 0.3);
//        }
//
//        show(resultMat, "result");
//
//        for (int i = 0; i < n; i++) {
//            int rv = (int) resultMat.get(i);
//            int v2 = (int) mat2.get(i);
//            resultMat.put(i, (rv - v2 * 0.3) / 0.7);
//        }
//
//        show(resultMat, "remove rain result");

        IplImage image = loadAndShowOrExit(new File("art5014360081-s.jpg"), CV_LOAD_IMAGE_COLOR);
        System.out.println(image.width());
        System.out.println(image.height());

        IplImage orgImage = loadAndShowOrExit(new File("P20111205Zgshy0081.jpg"), CV_LOAD_IMAGE_COLOR);


        IplImage smallOrgImage = IplImage.create(cvGetSize(image), image.depth(), image.nChannels());
        cvResize(orgImage, smallOrgImage);

        show(smallOrgImage, "small org image");

        CvMat imageMat = image.asCvMat();
        CvMat smallOrgMat = smallOrgImage.asCvMat();

        IplImage resultImage = IplImage.create(cvGetSize(image), image.depth(), image.nChannels());
        CvMat resultMat = resultImage.asCvMat();

        int n = imageMat.rows() * imageMat.cols() * imageMat.nChannels();

        for (int i = 0 ; i < n; i++) {
            double v = imageMat.get(i);
            double sv = smallOrgMat.get(i);
            double diff = sv * 0.95 - v;

            // 取出水印
            if (diff > 0 && i > 110 * 300 * 3 && i < 145 * 300 * 3 && Math.floor(i / 3.0) % 300 > 90 && Math.floor(i / 3.0) % 300 < 210) {
                // 去除绿色残留
                int tmpI = (int) (Math.ceil(i / 3.0) * 3 - 2);
                if(((i % 3 == 1 && diff < 15) || (i > tmpI && resultMat.get(tmpI) < 30))) {
                    System.out.println(diff + "," + tmpI   + "," + i + "," + resultMat.get(tmpI));
                    resultMat.put(i, 0);
                } else {
                    resultMat.put(i, diff + 15);
                }

                resultMat.put(i, diff + 15);
            } else {
                resultMat.put(i, 0);
            }

        }

        show(resultMat, "result image");

        for (int i = 0; i < n; i ++) {
            double v = imageMat.get(i);
            double rv = resultMat.get(i);
            resultMat.put(i,  v + rv);
        }

        show(resultMat, "result image");

//        IplImage image = loadAndShowOrExit(new File("art5014360081-s.jpg"), CV_LOAD_IMAGE_COLOR);
//
//
//        IplImage[] channels = ColorHistogram.splitChannels(image);
//        CvMat imageMat = channels[0].asCvMat();
//
//        IplImage resultImage = IplImage.create(cvGetSize(image), image.depth(), image.nChannels());
//        CvMat resultMat = resultImage.asCvMat();
//
//        int n = imageMat.rows() * imageMat.cols();
//        for (int i = 128 * 300; i < 130 * 300; i++) {
//            System.out.println(imageMat.get(i));
//        }
    }
}
